﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace Domain.Entities
{
    class Serializer
    {
        public List<RemoteDevice> RemoteDevices { get; private set; }
        public bool saveRemoteDevices(List<RemoteDevice> remoteDevices, string fileToSave)
        {
            fileToSave = String.Format("{0}.dat", fileToSave);
            BinaryFormatter binFormat = new BinaryFormatter();
            try
            {
                using (Stream fStream = new FileStream(fileToSave, FileMode.Create, FileAccess.Write, FileShare.None))
                {
                    binFormat.Serialize(fStream, remoteDevices);
                }
            }
            // обрабатывается выше
            catch (Exception)
            {
                throw;
            }
            return true;
        }
        /// <summary>
        /// Если возвращает true, тогда назначается св-во RemoteDevices
        /// </summary>
        /// <param name="fileToLoad"></param>
        /// <returns></returns>
        public bool loadRemoteDevices(string fileToLoad)
        {
            BinaryFormatter binFormat = new BinaryFormatter();
            try
            {
                using (Stream fStream = File.OpenRead(fileToLoad))
                {
                    RemoteDevices =
                         (List<RemoteDevice>)binFormat.Deserialize(fStream);
                }
            }
            // обрабатывается выше
            catch (Exception)
            {
                throw;
            }
            return true;
        }
    }
}
